home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / fileread.zip / TEST.PAS < prev    next >
Pascal/Delphi Source File  |  1992-04-16  |  4KB  |  120 lines

  1.  
  2. { **************************************************************************
  3.   *                                                                        *
  4.   *                          FileRead Test Program                         *
  5.   *                          =====================                         *
  6.   *                                                                        *
  7.   *  Description:                                                          *
  8.   *                                                                        *
  9.   *       This program will show you the various functions of the          *
  10.   *  TFread object.  With the TFread object, you create an instance        *
  11.   *  of the object, passing it a file name and the size of the records     *
  12.   *  in which you will be reading from and writing to the data file.       *
  13.   *  This program takes a record called InfoRec, initializes the           *
  14.   *  data, then writes 100 records to the file.  With the sequential       *
  15.   *  procedures (ReadNext and ReadPrevious) built into the TFread          *
  16.   *  object, this program will then seek the first record and read         *
  17.   *  until the last record is read, displaying the physical record         *
  18.   *  number and the name field of the record.  The program will then       *
  19.   *  read the file backwards starting from the last record and             *
  20.   *  stopping when the first record has been read and displayed.  The      *
  21.   *  program will then close the file and dispose of the object.           *
  22.   *                                                                        *
  23.   ************************************************************************** }
  24.  
  25. Program Test_FileRead;
  26.  
  27. uses
  28.   Crt,FileRead;
  29.  
  30. Type
  31.   { The information record we'll use for our data file. }
  32.   InfoRec = Record
  33.     Name : String[20];
  34.     Address : String[50];
  35.     City : String[25];
  36.     State : String[2];
  37.     Zip  : String[5];
  38.   End;
  39.  
  40. Procedure Main;
  41. var
  42.   Info   : InfoRec;    { variable of type inforec }
  43.   MyFile : TFreadPtr;  { an instance of the TFread object }
  44.   i      : integer;    { loop variable }
  45.   temp   : string;     { temp string for processing integers }
  46.   f      : file;       { temp file for deleting old version of TOM.GAG }
  47. Begin
  48.   { Prepare record for writing. }
  49.   FillChar(Info,SizeOf(Info),#0);
  50.   with Info do
  51.   begin
  52.     Address := '5861 Horseshoe Drive';
  53.     City:='Bethel Park';
  54.     State := 'PA';
  55.     Zip := '15102';
  56.   end;
  57.  
  58.   { Let's initialize a file object and create a new file. }
  59.   New(MyFile,Init('INFO.DAT',Create,SizeOf(InfoRec)));
  60.  
  61.   { Let's do some stuff with our new file. }
  62.   with Myfile^ do
  63.   begin
  64.     for i:=1 to 100 do
  65.     begin
  66.       { Modify name with the number of the logical record. }
  67.       Str(i,temp);
  68.       Info.Name:='Tom Clancy'+'  '+temp;
  69.       { Append record to end of file (or just add records in this case) }
  70.       AppendRec(Info);
  71.     end;
  72.     { Try to rename the file. }
  73.     if not RenameFile('TOM.GAG') then
  74.     begin
  75.       { If we cannot, then delete previous version of TOM.GAG. }
  76.       Assign(f,'TOM.GAG');
  77.       erase(f);
  78.       { And try one more time. }
  79.       if not RenameFile('TOM.GAG') then
  80.       begin
  81.         { if we cannot rename this time, then just give up. }
  82.         Writeln('Still having trouble renaming file!');
  83.         halt(1);
  84.       end;
  85.     end;
  86.     { If we can rename the file, then read the first record. }
  87.     ReadRec(Info,0);
  88.     if ReadError then
  89.       { if a read error occured, then just close and quit. }
  90.       writeln('Oops, dude, and error hath occurred!')
  91.     else
  92.     begin
  93.       { go through the file sequentially forward, printing the name
  94.         field of the particular record. }
  95.       while not ReadError do
  96.       begin
  97.         Writeln(GetCurrent:4,':  ',Info.Name);
  98.         ReadNext(Info);
  99.       end;
  100.       writeln;
  101.       write('Press a key to go backwards!');
  102.       readln;
  103.       writeln;
  104.       { Let's go backward now, starting with the last record which is
  105.         also the current record! }
  106.       ReadCurrent(Info);
  107.       while not ReadError do
  108.       begin
  109.         Writeln(GetCurrent:4,':  ',Info.Name);
  110.         ReadPrevious(Info);
  111.       end;
  112.     end;
  113.   end;
  114.   { Okay, close file and clean house. }
  115.   Dispose(Myfile,Done);
  116. End;
  117.  
  118. Begin
  119.   Main;
  120. End.